//
// Created by 12992 on 24-10-17.
//

// You may need to build the project (run Qt uic code generator) to get "ui_AudioEncodeConfigItem.h" resolved

#include "audioencodeconfigitem.h"
#include "ui_AudioEncodeConfigItem.h"

namespace gui::widgets
{
    AudioEncodeConfigItem::AudioEncodeConfigItem(const utils::audio_config& config, int streamId, QWidget* parent) :
            QWidget(parent), ui(new Ui::AudioEncodeConfigItem)
    {
        ui->setupUi(this);
        this->init_widget(0);
        this->init_signals();
        this->set_widgets(config);
    }

    AudioEncodeConfigItem::~AudioEncodeConfigItem()
    {
        delete ui;
    }

    void AudioEncodeConfigItem::init_widget(int streamId)
    {
        QStringList encodeList;
        encodeList << "AAC" << "MP3";
        this->ui->encodeCombo->addItems(encodeList);
        QStringList bitrateList;
        bitrateList << "128" << "192" << "256" << "320";
        this->ui->bitrateCombo->addItems(bitrateList);
        QStringList samplerateList;
        samplerateList << "44100" << "48000";
        this->ui->samplerateCombo->addItems(samplerateList);
        this->ui->streamId->setText(QString::number(streamId));
    }

    void AudioEncodeConfigItem::init_signals()
    {
        connect(this->ui->copyStream, &QCheckBox::checkStateChanged,
                this, &AudioEncodeConfigItem::changeCopyStream);
    }

    void AudioEncodeConfigItem::changeCopyStream(Qt::CheckState state)
    {
        switch (state)
        {
            case Qt::CheckState::Checked:
            case Qt::CheckState::PartiallyChecked:
            {
                this->ui->samplerateCombo->setDisabled(true);
                this->ui->bitrateCombo->setDisabled(true);
                this->ui->encodeCombo->setDisabled(true);
            }
                break;
            default:
            {
                this->ui->samplerateCombo->setEnabled(true);
                this->ui->bitrateCombo->setEnabled(true);
                this->ui->encodeCombo->setEnabled(true);
            }
        }
    }

    void AudioEncodeConfigItem::set_widgets(const utils::audio_config& config)
    {
        int index = this->ui->encodeCombo->findText(QString::fromStdString(config.get_encode()));
        index = (index == -1 ? 0 : index);
        this->ui->encodeCombo->setCurrentIndex(index);

        index = this->ui->samplerateCombo->findText(QString::number(config.get_sample_rete()));
        index = (index == -1 ? 0 : index);
        this->ui->samplerateCombo->setCurrentIndex(index);

        index = this->ui->bitrateCombo->findText(QString::number(config.get_bitrate()));
        index = (index == -1 ? 0 : index);
        this->ui->bitrateCombo->setCurrentIndex(index);
    }

    utils::audio_config AudioEncodeConfigItem::getConfig() const
    {
        int samplerate = this->ui->samplerateCombo->currentText().toInt();
        int bitrate = this->ui->bitrateCombo->currentText().toInt();
        std::string encode = this->ui->encodeCombo->currentText().toStdString();
        return utils::audio_config::config(encode, bitrate, samplerate);
    }
} // gui::widgets
